﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using MySql.Data.MySqlClient;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionLib.Dao
{
    public class DaoOrionMenu : DaoBase
    {
        public DaoOrionMenu()
            : base()
        {
            this.TableName = "OrionMenu";
            this.ViewName = "VOrionMenu";
        }

        public OrionMenu SelectById(Int32 OrionId)
        {
            lParam.Clear();
            OrionMenu retour = new OrionMenu();
            String req = this.DebutSelect
                + this.TableName
                + " WHERE OrionId=@orionid";

            lParam.Add(new MySqlParameter("@OrionId", OrionId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public List<OrionMenu> SelectAll(Int32 OrionMenuId)
        {
            lParam.Clear();
            List<OrionMenu> retour = new List<OrionMenu>();
            String req = this.DebutSelect
                + this.TableName
                 + " WHERE OrionMenuId=@OrionMenuId AND OrionMenuIdParent=0 AND OrionEtat=1 Order By Ordre";

            lParam.Add(new MySqlParameter("@OrionMenuId", OrionMenuId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();
            return retour;
        }

        public List<OrionMenu> SelectAllChild(Int32 OrionMenuIdParent)
        {
            lParam.Clear();
            List<OrionMenu> retour = new List<OrionMenu>();
            String req = this.DebutSelect
                + this.TableName
                 + " WHERE OrionMenuIdParent=@OrionMenuIdParent AND OrionEtat=1 Order By Ordre";

            lParam.Add(new MySqlParameter("@OrionMenuIdParent", OrionMenuIdParent));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();
            return retour;
        }

        public List<OrionMenu> SelectAll()
        {
            lParam.Clear();
            List<OrionMenu> retour = new List<OrionMenu>();
            String req = this.DebutSelect
                + this.TableName;

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();
            return retour;
        }

        public OrionMenu Insert(OrionMenu om)
        {
            lParam.Clear();
            om.OrionCrea = DateTime.Now;
            om.OrionMaj = DateTime.Now;

            OrionMenu retour = new OrionMenu();
            String req = "INSERT INTO " + TableName + "(OrionId, Libelle, Ordre, ClasseName, FormName, FormTitle, OrionMenuIdParent, OrionImageId, OrionParam, OrionMenuId, OrionCrea, OrionMaj, OrionUserId, OrionEtat)  VALUES(NULL," +
                " @Libelle, @Ordre, @ClasseName, @FormName, @FormTitle, @OrionMenuIdParent, @OrionImageId, @OrionParam, @OrionMenuId, @OrionCrea, @OrionMaj, @OrionUserId, @OrionEtat)";

            lParam.Add(new MySqlParameter("@Libelle", om.Libelle));
            lParam.Add(new MySqlParameter("@Ordre", om.Ordre));
            lParam.Add(new MySqlParameter("@ClasseName", om.ClasseName));
            lParam.Add(new MySqlParameter("@FormName", om.FormName));
            lParam.Add(new MySqlParameter("@FormTitle", om.FormTitle));
            lParam.Add(new MySqlParameter("@OrionMenuIdParent", om.OrionMenuIdParent));
            lParam.Add(new MySqlParameter("@OrionImageId", om.OrionImageId));
            lParam.Add(new MySqlParameter("@OrionParam", om.OrionParam));
            lParam.Add(new MySqlParameter("@OrionMenuId", om.OrionMenuId));
            lParam.Add(new MySqlParameter("@OrionCrea", om.OrionCrea));
            lParam.Add(new MySqlParameter("@OrionMaj", om.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionEtat", om.OrionEtat));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(Convert.ToInt32(this.LastInsertId));

            return retour;
        }

        public OrionMenu Update(OrionMenu om)
        {
            lParam.Clear();
            om.OrionMaj = DateTime.Now;

            OrionMenu retour = new OrionMenu();
            String req = "UPDATE " + TableName + " SET Libelle=@Libelle, Ordre=@Ordre, OrionImageId=@OrionImageId, ClasseName=@ClasseName, FormName=@FormName, FormTitle=@FormTitle, OrionMenuIdParent=@OrionMenuIdParent, OrionParam=@OrionParam, OrionMenuId=@OrionMenuId, OrionMaj=@OrionMaj, OrionUserId=@OrionUserId " +
                " WHERE OrionId=@OrionId";

            lParam.Add(new MySqlParameter("@OrionId", om.OrionId));
            lParam.Add(new MySqlParameter("@Libelle", om.Libelle));
            lParam.Add(new MySqlParameter("@Ordre", om.Ordre));
            lParam.Add(new MySqlParameter("@OrionImageId", om.OrionImageId));
            lParam.Add(new MySqlParameter("@ClasseName", om.ClasseName));
            lParam.Add(new MySqlParameter("@FormName", om.FormName));
            lParam.Add(new MySqlParameter("@FormTitle", om.FormTitle));
            lParam.Add(new MySqlParameter("@OrionMenuIdParent", om.OrionMenuIdParent));
            lParam.Add(new MySqlParameter("@OrionParam", om.OrionParam));
            lParam.Add(new MySqlParameter("@OrionMenuId", om.OrionMenuId));
            lParam.Add(new MySqlParameter("@OrionMaj", om.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(om.OrionId);

            return retour;
        }

        private OrionMenu ChargeFromReader(MySqlDataReader dr)
        {
            OrionMenu retour = new OrionMenu();

            if (dr.IsDBNull(0))
                retour.OrionId = 0;
            else
                retour.OrionId = dr.GetInt32("OrionId");
            if (dr.IsDBNull(1))
                retour.Libelle = string.Empty;
            else
                retour.Libelle = dr.GetString("Libelle");
            if (dr.IsDBNull(2))
                retour.Ordre = 0;
            else
                retour.Ordre = dr.GetInt32("Ordre");
            if (dr.IsDBNull(3))
                retour.OrionImageId = 0;
            else
                retour.OrionImageId = dr.GetInt32("OrionImageId");
            if (dr.IsDBNull(4))
                retour.ClasseName = string.Empty;
            else
                retour.ClasseName = dr.GetString("ClasseName");
            if (dr.IsDBNull(5))
                retour.FormName = string.Empty;
            else
                retour.FormName = dr.GetString("FormName");
            if (dr.IsDBNull(6))
                retour.FormTitle = string.Empty;
            else
                retour.FormTitle = dr.GetString("FormTitle");
            if (dr.IsDBNull(7))
                retour.OrionParam = string.Empty;
            else
                retour.OrionParam = dr.GetString("OrionParam");
            if (dr.IsDBNull(8))
                retour.OrionMenuIdParent = 0;
            else
                retour.OrionMenuIdParent = dr.GetInt32("OrionMenuIdParent");
            if (dr.IsDBNull(9))
                retour.OrionMenuId = 0;
            else
                retour.OrionMenuId = dr.GetInt32("OrionMenuId");

            retour.OrionCrea = dr.GetDateTime("OrionCrea");
            retour.OrionMaj = dr.GetDateTime("OrionMaj");
            retour.OrionUserId = dr.GetInt32("OrionUserId");
            retour.OrionEtat = dr.GetBoolean("OrionEtat");

            return retour;
        }
    }
}
